class Item < ActiveRecord::Base
  has_and_belongs_to_many :effects
  has_and_belongs_to_many :properties, :conditions => "published = 1"
  belongs_to :group
  belongs_to :marketgroup
  has_one :blueprint
  has_many :materials, :conditions => "activity = 1 AND quantity > 0"
  has_many :sell_prices, :class_name => "Offer", :conditions => "sell = 1"
  has_many :buy_prices, :class_name => "Offer", :conditions => "sell = 0"
    
  attr_accessor :total_isk
  
  def self.search(query)
    self.find(:all, :conditions => [ 'LOWER(name) LIKE ?', '%' + query.downcase + '%' ])
  end
  
  def self.search_blueprint(query)
    self.find_all([ "LOWER(name) LIKE ? and LOWER(name) LIKE '%blueprint%'", '%' + query.downcase + '%' ])
  end
  
  def calculate_minerals(me, pe)
    m = ProductionMaterials.new(0) # strange ...


    self.materials.each do |mat|
      id = mat.requiredType_id.to_int

#      m[id] = Hash.new
#      m[id]["type"] = mat.item.name
      
      case mat.item.group.category.name
        when "Skill"
          m.skills[id] = Component.new(id, mat.item.name,0,0)

        when "Material"
          waste = mat.quantity * (0.1 / ( me.to_i + 1 ))
          m.minerals[id] = Component.new()
          m.minerals[id].name = mat.item.name
          m.minerals[id].id = mat.item.id
#          logger.debug m.minerals[id]
          if(waste > 0.5)
            m.minerals[id].amount = (mat.quantity +  (mat.quantity * (0.25 - (0.05 * pe))) + waste).round 
          else 
            m.minerals[id].amount = mat.quantity
          end
            if mat.item.price.nil?
              mat.item.price = Price.new(:price => MarketPrice.get_median_sell(mat.item.id))
            end
            m.minerals[id].price = m.minerals[id].amount * mat.item.price.price
            m.total +=  m.minerals[id].price
        else
          m.components[id] = Component.new(id, mat.item.name)
          m.components[id].amount = mat.quantity
          begin
            tmpm = Item.search(mat.item.name + " Blueprint")[0].calculate_minerals(me, pe)
          rescue NoMethodError
            if mat.item.price.nil?
              mat.item.price = Price.new(:price => MarketPrice.get_median_sell(mat.item.id))
            end
            m.components[id].amount = mat.quantity
            m.components[id].price = m.components[id].amount * mat.item.price.price
            m.total +=  m.components[id].price
          else
            m.components[id] = m
            m.total += m.components[id].total
          end
      end
    end
    return m
  end
   
  def calculate_me(till, pe)
    me = Array.new
    0.upto(till) do |i|
      m = self.calculate_minerals(i, pe)

      last = m.total if last == nil 
#      next if last == m.total && i != 0
#            logger.debug i.to_s + " => " + self.calculate_minerals(i, pe).total.to_s
      me[i] = { :me => i, :cost => m.total, :improvement => last - m.total }
#      last =  m.total if m.total != last
    end
      return me
  end
  

end



Component = Struct.new( "Data", :id, :name, :amount, :price)

class ProductionMaterials
  attr_accessor :skills, :minerals, :ships, :components, :modules, :total
  
  def initialize(tmp)
    @skills = Hash.new
    @minerals = Hash.new
    @ships = Hash.new
    @components = Hash.new
    @total = 0
  end
  
  
end

